﻿using System.Windows.Forms;
using Engine;
using Engine.Cameras;
using EngineDrawer;
using EngineDrawer.Debugging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using ThinkDotNetEditorLib;
using ThinkDotNetEngineDrawer;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;

namespace ThinkDotNetEditor
{
    public class GameEditor : Game,IGameEditor
    {
        public Control ParentControl { get; private set; }
        public World World
        {
            get { return (World) Services.GetService(typeof (World)); }
            set 
            {                 
                WorldDrawer worldDrawer = (WorldDrawer) Services.GetService(typeof (WorldDrawer));
                worldDrawer.World = value;
            }
        }
        public Game XnaGame { get { return this; } }
        private readonly GraphicsDeviceManager graphics;
        private readonly Form _parentForm;

        public GameEditor(Control parentControl)
        {
            ServiceLocator.Instance.RegisterInstance(this);
            ParentControl = parentControl;
            graphics = new GraphicsDeviceManager(this);
            graphics.PreparingDeviceSettings += GraphicsPreparingDeviceSettings;
            _parentForm = ParentControl.FindForm();
            Content.RootDirectory = "Content";            
        }
        public void Reset()
        {
            graphics.PreferredBackBufferHeight = ParentControl.Height;
            graphics.PreferredBackBufferWidth = ParentControl.Width; 
            graphics.ApplyChanges();

        }
        private void GraphicsPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = ParentControl.Width;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = ParentControl.Height;
            e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = ParentControl.Handle;

        }
        protected override void Initialize()
        {
            BaseCamera fppCamera = new EditorCamera(this);
            Services.AddService(typeof(BaseCamera), fppCamera);
            Components.Add(fppCamera);
            Components.Add(new ArenaDrawer(this,null));
            Components.Add(new BoundingBoxDrawer(this));
            
            #if DEBUG
       
            Components.Add(new FpsDrawer(this));

            #endif

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {            
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if(!_parentForm.IsDisposed)
                Mouse.WindowHandle = ParentControl.Handle;
            if (Control.FromHandle(graphics.GraphicsDevice.PresentationParameters.DeviceWindowHandle) == null)
            {
                Exit();
                return;
            }
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            Control xnaWindow = Control.FromHandle((Window.Handle));
            xnaWindow.Visible = false;
  
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
